package me.hgj.mvvmhelper.widget.state;

import android.view.View;

public class ClickProxy implements View.OnClickListener {

    private View.OnClickListener origin;
    private long lastclick = 0;
    private long timems = 500; //默认点击时间间隔 500ms
    private IAgain mIAgain;

    /**
     *  点击事件代理构造
     * @param origin ${timems} 时间内单次点击事件处理回调
     * @param timems 点击时间间隔
     * @param again  ${timems} 时间内多次点击事件处理回调
     */
    public ClickProxy(View.OnClickListener origin, long timems, IAgain again) {
        this.origin = origin;
        this.mIAgain = again;
        this.timems = timems;
    }

    /**
     *  点击事件代理构造
     * @param origin ${timems} 时间内多次点击事件处理回调
     */
    public ClickProxy(View.OnClickListener origin) {
        this.origin = origin;
    }


    /**
     * 点击事件代理构造
     * @param origin ${timems} 时间内多次点击事件处理回调
     * @param timems 点击事件允许时间间隔
     */
    public ClickProxy(View.OnClickListener origin, long timems) {
        this.origin = origin;
        this.timems = timems;
    }


    @Override
    public void onClick(View v) {
        if (System.currentTimeMillis() - lastclick >= timems) {
            origin.onClick(v);
            lastclick = System.currentTimeMillis();
        } else {
            if (mIAgain != null) mIAgain.onAgain();
        }
    }

    public interface IAgain {
        void onAgain();//重复点击
    }
}
